package org.rabbitmq.rabbitextensions.listener;

import com.rabbitmq.client.Channel;
import org.rabbitmq.rabbitextensions.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

//@Component
public class RetryListener {
//    @RabbitListener(queues = Constants.RETRY_QUEUE)
//    public void HanderMessage(Message message) throws UnsupportedEncodingException {
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        System.out.printf("收到消息: %s ,deleteTag: %s \n",new String(message.getBody(),"UTF-8"),deliveryTag);
//        int t = 3/0;
//        System.out.println("业务处理");
//    }

    @RabbitListener(queues = Constants.RETRY_QUEUE)
    public void HanderMessage(Message message,Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try{
            System.out.printf("收到消息: %s ,deleteTag: %s \n",new String(message.getBody(),"UTF-8"),deliveryTag);
            int t = 3/0;
            System.out.println("业务处理");
            //确定确认:
            channel.basicAck(deliveryTag,false);
        }catch(Exception e){
            channel.basicNack(deliveryTag,false,true);
        }
    }

}
